package com.tte.common.designpattern.nullObject;

/**
 * 创建一个定义操作（在这里，是客户的名称）的 AbstractCustomer 抽象类，和扩展了 AbstractCustomer 类的实体类。工厂类 CustomerFactory 基于客户传递的名字来返回 RealCustomer 或 NullCustomer 对象
 */
public class NullPatternDemo {
    public static void main(String[] args) {

        AbstractCustomer customer1 = CustomerFactory.getCustomer("张三");
        AbstractCustomer customer2 = CustomerFactory.getCustomer("李四");
        AbstractCustomer customer3 = CustomerFactory.getCustomer("王五");
        AbstractCustomer customer4 = CustomerFactory.getCustomer("赵六");

        System.out.println("客户");
        System.out.println(customer1.getName());
        System.out.println(customer2.getName());
        System.out.println(customer3.getName());
        System.out.println(customer4.getName());
    }
}
/**
 * 抽象客户类
 */
abstract class AbstractCustomer {
    protected String name;
    public abstract boolean isNil();
    public abstract String getName();
}

class RealCustomer extends AbstractCustomer {

    public RealCustomer(String name) {
        this.name = name;
    }

    @Override
    public String getName() {
        return name;
    }

    @Override
    public boolean isNil() {
        return false;
    }
}

class NullCustomer extends AbstractCustomer {

    @Override
    public String getName() {
        return "没有客户数据！！！";
    }

    @Override
    public boolean isNil() {
        return true;
    }
}
/**
 *
 */
class CustomerFactory {

    public static final String[] names = {"张三", "李四", "王五"};

    public static AbstractCustomer getCustomer(String name){
        for (int i = 0; i < names.length; i++) {
            if (names[i].equalsIgnoreCase(name)){
                return new RealCustomer(name);
            }
        }
        return new NullCustomer();
    }
}